package test;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.cxn.tools.commons.poi.excel.MyHSSFCell;
import org.cxn.tools.commons.poi.excel.MyHSSFCellStyle;
import org.cxn.tools.commons.poi.excel.MyHSSFRow;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: lenovo
 * Date: 11-12-22
 * Time: 下午3:18
 * To change this template use File | Settings | File Templates.
 * 对excel的学习,步骤分解练习
 */
public class Demo2 {
    public static void main(String[] args) {
        try {
//            createExcel();
//            createSheet();
            createCell();

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    /**
     * 创建新excel
     */
    public final static void createExcel() throws IOException {
        Workbook wb = new HSSFWorkbook();
//        Workbook wb = new XSSFWorkbook();
        outFile(wb, "创建一个空的excel");
    }

    /**
     * 创建 sheet
     *
     * @throws IOException
     */
    public final static void createSheet() throws IOException {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet1 = wb.createSheet("new sheet");
        Sheet sheet2 = wb.createSheet("new sheet2");
        outFile(wb, "创建一个带标签页的excel");

    }

    /**
     * 创建 单元格
     *
     * @throws IOException
     */
    public final static void createCell() throws IOException {
        Workbook wb = new HSSFWorkbook();
        CreationHelper creationHelper = wb.getCreationHelper();
        Sheet sheet1 = wb.createSheet("new sheet");
        // 创建一行
        Row row = MyHSSFRow.getInstance().createRow(sheet1, 1);
        //创建一个单元格 第一行第一列
        Cell cell = MyHSSFCell.getInstance().createCell(row, "a");
        // 赋值
        cell.setCellValue("三炮");

        //第1行第2个格子赋值和创建
        MyHSSFCell.getInstance().createCell(row, "b").setCellValue("asdsad");
        //第1行第3个格子赋值和创建
        MyHSSFCell.getInstance().createCell(row, "c").setCellValue(
                creationHelper.createRichTextString("asdasd"));
        //第1行第4个格子赋值和创建
        MyHSSFCell.getInstance().createCell(row, "d").setCellValue(true);
        //第1行第5个格子赋值和创建 日期类型
        MyHSSFCell.getInstance().createCell(row, "e").setCellValue(new Date());
        //第1行第6个格子赋值和创建 日期类型
        cell = MyHSSFCell.getInstance().createCell(row, "f");
        cell.setCellValue(new Date());
        cell.setCellStyle(MyHSSFCellStyle.getInstance().load(wb.createCellStyle())
                .addDateStyle(creationHelper, "m/d/yy h:mm").getStyle());
        //第1行第7个格子赋值和创建 日期类型 也可以用 java.util.Calendar
        cell = MyHSSFCell.getInstance().createCell(row, "g");
        cell.setCellValue(Calendar.getInstance());
        cell.setCellStyle(MyHSSFCellStyle.getInstance().getStyle());

        outFile(wb, "创建单元格excel");

    }

    public final static void outFile(Workbook wb, String fileName) throws IOException {
        FileOutputStream outputStream = new FileOutputStream("C:/xuexihuanjing/" + fileName + ".xls");
        wb.write(outputStream);
        outputStream.close();
    }

}